#include "StdAfx.h"
#include "MeasureSolution.h"
#include "Measure.h"

CMeasureSolution::CMeasureSolution(void)
  : active_project_index_ (kInvalidIndex)
  , valid_(false)
{
  SyncActiveProjectIndex();
}


CMeasureSolution::~CMeasureSolution(void)
{
}

long CMeasureSolution::AddProject(const ProjectTag& tag)
{
  for (int n=0; n<NumProjects(); ++n) {
    if (tag.name == projects_[n].name) {
      return CErrorCode::kProjectNameConflict;
    }
  }

  projects_.push_back(tag);

  return CErrorCode::kOk;
}

long CMeasureSolution::RemoveProject(const tstring& name)
{
  if (active_project_name() == name) {
    return CErrorCode::kActiveProjectCantDelete;
  }

  for (int n=0; n<NumProjects(); ++n) {
    if (name == projects_[n].name) {
      projects_.erase(projects_.begin()+n);
      SyncActiveProjectIndex();

      return CErrorCode::kOk;
    }
  }

  return CErrorCode::kProjectNotFound;
}

long CMeasureSolution::Clear(void)
{
  name_.clear();
  projects_.clear();
  active_project_name_.clear();
  cameras_.Clear();
  active_project_index_ = -1;

  return CErrorCode::kOk;
}

long CMeasureSolution::ActivateProject(int index)
{
  if (index<0 || index>=NumProjects()) {
    return CErrorCode::kIndexOutOfRange;
  }

  active_project_index_ = index;
  active_project_name_  = projects_[index].name;

  return CErrorCode::kOk;
}

long CMeasureSolution::ActivateProject(const tstring& name)
{
  if (active_project_name() == name) {
    return CErrorCode::kAlreadyActivated;
  }

  for (int index=0; index<NumProjects(); ++index) {
    if (name == projects_[index].name) {
      active_project_index_ = index;
      active_project_name_  = name;

      return CErrorCode::kOk;
    }
  }

  return CErrorCode::kProjectNotFound;
}

void CMeasureSolution::SyncActiveProjectIndex(void)
{
  active_project_index_ = kInvalidIndex;
  for (int index=0; index<NumProjects(); ++index) {
    if (active_project_name() == project(index).name) {
      active_project_index_ = index;

      return;
    }
  }
}

CCamera* CMeasureSolution::FindCamera(const tstring& camera_name) const
{
  for (int i=0; i<cameras_.num_cameras(); ++i) {
    if (camera_name == cameras_.camera(i)->Name()) {
      return cameras_.camera(i);
    }
  }

  return NULL;
}

int CMeasureSolution::FindProject(int catagory) const
{
  for (int i=0; i<NumProjects(); ++i) {
    if (project(i).catagory==catagory) {
      return i;
    }
  }

  return -1;
}

tstring CMeasureSolution::active_project_directory(void) const
{
  if (active_project_index_<0 || active_project_index_>=NumProjects()) {
    return tstring(_T(""));
  }

  boost::filesystem::path p(theApp.config().solution_path());
  p /= projects_[active_project_index_].name;

  return p.string();
}

tstring CMeasureSolution::active_project_file_path(void) const
{
  return active_project_directory() + _T("\\") + active_project_name() +
    _T(".measure.xml.project");
}

const CMeasureSolution::ProjectTag& CMeasureSolution::project(int index) const
{
  if (index<0 || index>=NumProjects()) {
    static const ProjectTag project_tag= {_T(""), 0, 0};
    return project_tag;
  }

  return projects_[index];
}
